<template>
  <div class="pet-info">
    <div class="tab-list flex-a">
      <div :class="tab == item.value ? 'active item' : 'item'" v-for="item in tabList" :key="item.value"
        @click="changeTab(item.value)">
        <span>{{ item.name }}</span>
        <i></i>
      </div>
    </div>
    <div class="box">
      <!-- 活体管理 -->
      <div v-if="tab == 'htgl'">
        <div class="flex-b">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" class="input"
            label-width="68px">
            <el-form-item>
              <el-select v-model="queryParams.varId" placeholder="请选择品种">
                <el-option v-for="item in pzList" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-select v-model="queryParams.state" placeholder="请选择活体状态">
                <el-option v-for="item in stateList" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-select v-model="queryParams.sex" placeholder="请选择性别">
                <el-option v-for="item in sexList" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-input v-model="queryParams.name" placeholder="请输入活体名称" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" class="btn" size="mini" @click="handleQuery">查询</el-button>
              <el-button type="primary" class="btn" size="mini" @click="handleAdd"
                v-hasPermi="['pet:petInfo:add']">新增活体</el-button>
              <!-- <el-button type="warning" class="btn" size="mini" @click="handleExport"
                v-hasPermi="['pet:petInfo:export']">导入</el-button> -->
              <!-- <el-button type="warning" class="btn" size="mini" @click="handleExport"
                v-hasPermi="['pet:petInfo:export']">导出</el-button> -->
            </el-form-item>
          </el-form>
        </div>
        <div>
          <el-table v-loading="loading" :data="petInfoList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="活体图片" align="center" prop="picture">
              <template slot-scope="scope">
                <img :src="scope.row.picture" style="width: 50%;" alt="">
              </template>
            </el-table-column>
            <el-table-column label="活体名称" align="center" prop="name" />
            <el-table-column label="活体品种" align="center" prop="varName" />
            <el-table-column label="性别" align="center" prop="sex">
              <template slot-scope="scope">
                {{ scope.row.sex==1?'弟弟':'妹妹' }}
              </template>
            </el-table-column>
            <el-table-column label="进货价" align="center" prop="inPrice" />
            <el-table-column label="零售价" align="center" prop="outPrice" />
            <el-table-column label="到店日期" align="center" prop="inDate" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.inDate, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <!-- <el-table-column label="售卖日期" align="center" prop="inDate" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.inDate, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column> -->
            <el-table-column label="毛色" align="center" prop="petColor" />
            <el-table-column label="活体状态" align="center" prop="state">
              <template slot-scope="scope">
                {{ scope.row.state==0?'未出售':scope.row.state==1?'已预定':scope.row.state==2?'已赠送':scope.row.state==3?'已夭折':scope.row.state==4?'已调拨':scope.row.state==5?'待领养':scope.row.state==6?'已领养':'' }}
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                  v-hasPermi="['pet:petInfo:edit']">编辑</el-button>
                <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                  v-hasPermi="['pet:petInfo:remove']">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="flex-b">
          <div>
            <el-button type="primary" class="btn" style="position:relative;top:15px">批量删除</el-button>
          </div>
          <div>
            <pagination :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
              @pagination="getList" />
          </div>
        </div>
      </div>
      <!-- 品种管理 -->
      <div v-if="tab == 'pzgl'">
        <varieties></varieties>
      </div>
    </div>
  </div>
</template>

<script>
import { listPetInfo, getPetInfo, delPetInfo, addPetInfo, updatePetInfo } from "@/api/pet/petInfo";
import { getVarList } from "@/api/pet/var"
import varieties from "./../var/index"
export default {
  name: "PetInfo",
  components: {
    varieties,
  },
  data() {
    return {
      tab: 'htgl',
      tabList: [
        {
          name: '活体管理',
          value: 'htgl'
        },
        {
          name: '品种管理',
          value: 'pzgl'
        },
      ],
      pzList: [],
      stateList: [
        {
          value: 0,
          label: '未出售'
        },
        {
          value: 1,
          label: '已预定'
        },
        {
          value: 2,
          label: '已赠送'
        },
        {
          value: 3,
          label: '已夭折'
        },
        {
          value: 4,
          label: '已调拨'
        },
        {
          value: 5,
          label: '待领养'
        },
        {
          value: 6,
          label: '已领养'
        }
      ],
      sexList: [
        {
          value: 0,
          label: '弟弟'
        },
        {
          value: 1,
          label: '妹妹'
        },
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活体信息表格数据
      petInfoList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        varId: null,
        inPrice: null,
        outPrice: null,
        sex: null,
        petColor: null,
        weight: null,
        high: null,
        date: null,
        state: null,
        inDate: null,
        source: null,
        picture: null,
        updateUser: null,
        createUser: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getVarList()//获取品种下拉
  },
  methods: {
    /** 查询活体信息列表 */
    getList() {
      this.loading = true;
      listPetInfo(this.queryParams).then(response => {
        this.petInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //获取品种下拉
    getVarList() {
      getVarList().then(res => {
        if (res.code == 200) {
          this.pzList = res.data
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        varId: null,
        inPrice: null,
        outPrice: null,
        sex: null,
        petColor: null,
        weight: null,
        high: null,
        date: null,
        state: null,
        inDate: null,
        source: null,
        remark: null,
        picture: null,
        updateUser: null,
        updateTime: null,
        createUser: null,
        createTime: null
      };
      this.resetForm("form");
    },
    // tab切换
    changeTab(value) {
      this.tab = value
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({ path: '/goods/living', query: {} })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({ path: '/goods/living', query: { id: row.id } })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePetInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPetInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除活体信息编号为"' + ids + '"的数据项？').then(function () {
        return delPetInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pet/petInfo/export', {
        ...this.queryParams
      }, `petInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
